<?php
abstract class eCMSFileModuleController extends eCMSModuleController {

	var $pattern = "#([0-9]*)_([0-9]*)_(.*)\\.(.+)#";
	
	/* Page Handling Functions (mostly ajax) */
	public function addFile() {
		
		//Get MaxID
		$pk = $this->getPageRequest()->getGetVar("pk".$this->getModuleName());
		$path = $this->getDataDir();

		$curr_files = $this->listFiles();
		$max=0;
		while(list($k, $v)=each($curr_files)) {
			if($max<$v["id"]) { $max = $v["id"]; $k=$k;}
		}
		
		$files = $this->getPageRequest()->getFiles();
		$file = $files["Filedata"];
			
		$tmp_file = $file['tmp_name'];
		$filename  = utf8_decode($file["name"]);
		$new_file = $pk."_".($max+1)."_".Utils::replaceUmlaute($filename);
		
		$fullpath = $path . "/" . $new_file;

		if(move_uploaded_file($tmp_file, $fullpath)) {
			$this->logEvent("AddFile",__FILE__, __CLASS__, __FUNCTION__, __LINE__, array("tmpfile"=>$tmp_file, "destfile"=>$fullpath));
		}
		else {
			$this->logEvent("AddFileFailed",__FILE__, __CLASS__, __FUNCTION__, __LINE__, array("tmpfile"=>$tmp_file, "destfile"=>$fullpath));
		}
		
		$this->setJsonResponse($this->listFiles());

	}
	
	public function deleteFile($deleteAll = false) {

		$path = $this->getDataDir();
		$pk = $this->getPageRequest()->getGetVar("pk".$this->getModuleName());
		$id = $this->getPageRequest()->getGetVar("id");
		
		$files = $this->listFiles();
		while(list($k, $v)=each($files)) {
			if($v["pk"]==$pk && ($v["id"]==$id) || $deleteAll) {
				$file = $pk."_".$id."_".$v["name"].".".$v["ext"];
				$ok = @unlink($path."/".$file);
				$k=$k;
			}
		}
		
		if($ok) {
			$this->logEvent("DeleteFile",__FILE__, __CLASS__, __FUNCTION__, __LINE__);;
		}
		else {
			$this->logEvent("DeleteFileFailed",__FILE__, __CLASS__, __FUNCTION__, __LINE__, array("path" => $path, "file" => $file));
		} 
		
		$this->setJsonResponse($this->listFiles());
	}		
	
	public function deleteAllFiles() {
		$this->deleteFile(true);
	}
	
	public function getFile() {

		$path = $this->getDataDir();
		$id = $this->getPageRequest()->getGetVar("id");
		
		$files = $this->listFiles();
		while(list($k, $v)=each($files)) {
			if($v["id"]==$id) {
				readfile($path . $v["file"]);
			}
		}
	}

	protected function listFiles() {

		$pkVariableName = $this->getPageRequest()->getGetVar("pk".$this->getModuleName());
		$path = $this->getDataDir();

		$files = array();
		
		$handle=@opendir($path); 
		
		/* This is the correct way to loop over the directory. */ 
		while (false !== ($file = @readdir($handle))) {  
			if($file!=".." && $file!=".") {
		    	preg_match_all($this->pattern, $file, $res);
		    	
		    	if((is_array($res[0]) && count($res[0]) > 0) &&  ($pkVariableName==(int)$res[1][0] && (int)$res[2][0]>0)) {
			    	$f["pk"]   = (int)$res[1][0];
			    	$f["id"]   = (int)$res[2][0];
			    	$f["name"] = $res[3][0];
			    	$f["ext"]  = $res[4][0];
			    	$f["file"] = $file;
			    	array_push($files,$f);	
		    	}	    	
		    }
		}

		return $files;
	}		
	
}